package com.woniuxy.rabbit.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.rabbit.pojo.FinanceMessage;
import com.woniuxy.rabbit.service.FinanceMessageService;
import com.woniuxy.rabbit.mapper.FinanceMessageMapper;
import com.woniuxy.rabbit.utils.MessageUtil;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author 76169
* @description 针对表【finance_message】的数据库操作Service实现
* @createDate 2024-12-25 10:32:03
*/
@Service
public class FinanceMessageServiceImpl extends ServiceImpl<FinanceMessageMapper, FinanceMessage>
    implements FinanceMessageService{

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private StringRedisTemplate redisTemplate;
    @Override
    public void sendMessage() {
        //需求：发送失败的消息，超过10次就放弃发送
        List<FinanceMessage> list = lambdaQuery().ne(FinanceMessage::getState, 1)//赛选状态不为1
                .le(FinanceMessage::getCount, 10) //尝试次数小于10次不发送,赛选小于10
                .list();
        for (FinanceMessage financeMessage : list) {
            log.debug("发送消息:{}");
            financeMessage.setCount(financeMessage.getCount()+1);//尝试次数
            financeMessage.setState(1); //发送
            updateById(financeMessage);
            MessageUtil.sendMessage(financeMessage.getExchange(),
                    financeMessage.getRoutingKey(),
                    financeMessage.getMessage(),
                    financeMessage.getId().toString(),
                    rabbitTemplate,redisTemplate);
        }
    }
}




